'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 道具加速使用 (建筑， 科技， 铁匠铺)
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var Code = require('../../../consts/code');
var PlayerManager = require('../../../libs/playerManager');
var dataApi = require('../../../libs/dataApi');
var rewardService = require('../../../services/rewardService');
var propertyConf = require('../../../config/propertyConf');
var roleService = require('../../../services/roleService');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var consumeConf = require('../../../config/consumeConf.js');
var buildModel = require('../../../models/buildModel.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 道具加速使用 (建筑， 科技， 铁匠铺)
 */
Handler.prototype.diamondQuick = function (msg, session, next) {
    var role_id = session.uid;
    var quick_consume_id = msg.quick_consume_id;
    var quick_type = msg.quick_type;
    var share_id = msg.share_id;
    var quick_list;
    var sub_diamond = 0;
    var consume_quick_time = 0;
    // PS quick_type   : -----    1：建筑   2：科技   3：铁匠铺
    if (!quick_type || !quick_consume_id || !share_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }
    // jshint maxstatements:150
    var build_model;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'vip', 'build', 'task', 'daily_task', 'achieve', 'tech', 'smithy', 'unionTask', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;

                // 建筑加速道具使用
                if (quick_type == 1) {

                    // 建筑是否在队列中
                    build_model = player_manager.build;
                    var build_id = share_id;

                    if (!build_model.getBuildSchedule(build_id)) {
                        return next(null, {
                            code: Code.BUILD_UPGRADE_TIME_END
                        });
                    }

                    // 队列是否已完成
                    var build_end_time = build_model.getBuildSchedule(build_id).end_time;
                    var build_now_time = moment().unix();
                    if (build_end_time < build_now_time) {
                        return next(null, {
                            code: Code.BUILD_UPGRADE_TIME_END
                        });
                    }

                    // 道具卡加速时间(配置表)
                    consume_quick_time = consumeConf.getConsumeTypeConf(quick_consume_id).value;
                    sub_diamond = consumeConf.getConsumeTypeConf(quick_consume_id).price;

                    // 扣除钻石
                    if (!roleService.subDiamond(player_manager, moduleType.MDL_BUILD, operationType.OPR_BUILD_CLEAR_CD, sub_diamond)) {
                        return next(null, {
                            code: Code.USER_DIAMOND_NOT_ENOUGH
                        });
                    }

                    if (!build_model.setBuildSchedule(build_id, consume_quick_time)) {
                        return next(null, {
                            code: Code.BUILD_NOT_IN_BUILDING
                        });
                    }
                    quick_list = build_model.getBuildSchedule(build_id);
                }

                // 科技加速道具使用
                if (quick_type == 2) {

                    // 是否有科技在升级中
                    var tech_model = player_manager.tech;
                    var tech_now_time = moment().unix();

                    if (tech_model.up_tech_end_tm < tech_now_time) {
                        return next(null, {
                            code: Code.TECH_QUE_EMPTY
                        });
                    }

                    if (tech_model.up_tech_id !== share_id) {
                        return next(null, {
                            code: Code.TECH_QUE_EMPTY
                        });
                    }

                    // 扣除钻石
                    // todo: 未配置暂定0钻石
                    sub_diamond = consumeConf.getConsumeTypeConf(quick_consume_id).price;
                    if (!roleService.subDiamond(player_manager, moduleType.MDL_BUILD, operationType.OPR_BUILD_CLEAR_CD, sub_diamond)) {
                        return next(null, {
                            code: Code.USER_DIAMOND_NOT_ENOUGH
                        });
                    }

                    // 道具卡加速时间(配置表)
                    consume_quick_time = consumeConf.getConsumeTypeConf(quick_consume_id).value || 0;

                    // 加速科技时间
                    var end_time = tech_model.up_tech_end_tm - consume_quick_time;
                    tech_model.setUpgradeTime(end_time);
                    var item = {
                        tech_id: tech_model.up_tech_id,
                        tech_end_time: tech_model.up_tech_end_tm
                    };
                    quick_list = item;
                }

                // 联盟任务加速
                if (quick_type == 3) {
                    var union_task_model = player_manager.unionTask;
                    var union_task_id = share_id;

                    if (!union_task_model.isTaskReceiveTwo(union_task_id)) {
                        return next(null, {
                            code: Code.UNION_USER_NOT_UNIONTASK
                        });
                    }

                    // 扣除钻石
                    sub_diamond = consumeConf.getConsumeTypeConf(quick_consume_id).price;
                    if (!roleService.subDiamond(player_manager, moduleType.MDL_BUILD, operationType.OPR_BUILD_CLEAR_CD, sub_diamond)) {
                        return next(null, {
                            code: Code.USER_DIAMOND_NOT_ENOUGH
                        });
                    }

                    // 道具卡加速时间(配置表)
                    consume_quick_time = consumeConf.getConsumeTypeConf(quick_consume_id).value || 0;
                    union_task_model.quickTaskConsume(union_task_id, consume_quick_time);
                    quick_list = union_task_model.unionTask_list;
                }
                cb();
            },
            function (cb) {
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    quick_consume_id: quick_consume_id,
                    quick_type: quick_type,
                    share_id: share_id,
                    quick_list: quick_list,
                    sub_diamond: sub_diamond
                }
            });
        }
    );
};
